package cn.mastor.controller;

import cn.mastor.service.IParticularsService;
import cn.mastor.domain.Particulars;
import cn.mastor.query.ParticularsQuery;
import mihoyo.result.JSONResult;
import mihoyo.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/particulars")
public class ParticularsController {

    @Autowired
    public IParticularsService particularsService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Particulars particulars){
        if(particulars.getId()!=null){
            particularsService.updateById(particulars);
        }else{
            particularsService.insert(particulars);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        particularsService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(particularsService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(particularsService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody ParticularsQuery query){
        Page<Particulars> page = new Page<Particulars>(query.getPage(),query.getRows());
        page = particularsService.selectPage(page);
        return JSONResult.success(new PageList<Particulars>(page.getTotal(),page.getRecords()));
    }
}
